package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    //添加要完善的话，也可以用和Delete相同地方法
    public void work(BookList bookList) {
        System.out.println("==删除图书==");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书名字:>");
        String name = scanner.nextLine();

        int index=-1;
        int currentUsedSize = bookList.getUsedSize();
        for (int i = 0; i < currentUsedSize; i++) {
            Book book = bookList.getPosBooks(i);
            if(book.getName().equals(name)) {//字符串的比较，用equals
                index=i;
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要删除的书！");
        } else {
            int start=index+1;
            while(start<currentUsedSize) {
                Book book = bookList.getPosBooks(start);
                bookList.setPosBooks(start-1,book);
                start++;
            }
            bookList.setPosBooks(currentUsedSize-1,null);
            bookList.setUsedSize(currentUsedSize-1);
            System.out.println("删除成功！");
        }
    }
}
